﻿using Code.Events;
using github.heerheer.card2.Code.Helper;
using github.heerheer.card2.Code.ImageHelper;
using HeerCards.Helper;
using HeerCards.Info;
using Native.Sdk.Cqp;
using Native.Sdk.Cqp.EventArgs;
using Native.Sdk.Cqp.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace Code
{
    public class ReceivePrivateMessage : IPrivateMessage
    {
        public void PrivateMessage(object sender, CQPrivateMessageEventArgs e)
        {
            foreach (var item in MessageEvents.MessageEventsContainer)
            {
                if (item.Key == e.Message)
                {
                    item.Value.Process(e);
                }
            }
        }
    }

    public class AppEnableEvent : IAppEnable
    {
        public void AppEnable(object sender, CQAppEnableEventArgs e)
        {
            //AppMain.GobalSetting = null;
            AppMain.Common = e.CQApi;

            AppMain.commandHelper = new CommandHelper();
            AppMain.GobalSetting = new GobalSetting();

            var path = e.CQApi.AppDirectory.Replace("data\\app\\github.heerheer.card2\\", "app\\github.heerheer.card2.cpk");//得到引用目录
            //自动更新
            //已删除

            try
            {
                AppReloader.LoadVIP();
                e.CQLog.Info("数据读取", "登入QQ：", e.CQApi.GetLoginQQ().Id.ToString());
                e.CQLog.Info("数据读取", "授权状态：",AppMain.VipInfo.isVIP.ToString()+ AppMain.VipInfo.VIPName);
                //读取卡池
                AppReloader.LoadPools();
                StringBuilder sb = new StringBuilder();
                AppMain.Pools.ForEach((HeerCards.HCPool pool) => { sb.Append(pool.Message); sb.Append(";"); });
                e.CQLog.Info("数据读取", "读取了", AppMain.Pools.Count, "个卡池");
                e.CQLog.Info("数据读取", "语句触发语句：", sb.ToString());
                //读取全局设置
                AppReloader.LoadGobal();
                e.CQLog.Info("数据读取", "读取全局设置成功");
                //读取货币单元
                AppReloader.LoadCosts();
                //创建image目录
                if (!Directory.Exists(e.CQApi.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2")))
                {
                    Directory.CreateDirectory(e.CQApi.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2"));
                    e.CQLog.Info("数据读取", "创建图片文件夹成功");
                }
                if (!Directory.Exists(e.CQApi.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2\\temp")))
                {
                    Directory.CreateDirectory(e.CQApi.AppDirectory.Replace("app\\github.heerheer.card2", "image\\card2\\temp"));
                    e.CQLog.Info("数据读取", "创建缓存文件夹成功");
                }
                //创建saves目录
                if (!Directory.Exists(e.CQApi.AppDirectory + "saves"))
                {
                    Directory.CreateDirectory(e.CQApi.AppDirectory + "saves");
                    e.CQLog.Info("数据读取", "创建群员数据文件夹成功");
                }
                //创建DLC目录
                if (Directory.Exists(e.CQApi.AppDirectory + "ImageDLC"))
                {
                    Directory.Delete(e.CQApi.AppDirectory + "ImageDLC");
                }
                AppReloader.LoadImageDLC();
            }
            catch (Exception ex)
            {
                e.CQLog.Error("自定义抽卡-启用错误", ex.Message);
            }
            //加入消息类
            MessageEvents.addMessageEvent(new ME_info());//info信息
            MessageEvents.addMessageEvent(new ME_reload());
            MessageEvents.addMessageEvent(new ME_help());
            MessageEvents.addMessageEvent(new ME_TestMe());
            MessageEvents.addMessageEvent(new Event_Bag());//背包系统
            MessageEvents.addMessageEvent(new Event_PoolInfo());//查看卡池信息
            MessageEvents.addMessageEvent(new Event_Checkin());//签到
            //MessageEvents.addMessageEvent(new Event_Checkin());
            //实例化语义文件
            AppMain.i18NHelper = new I18nHelper();
            AppMain.i18NHelper.setPath(AppMain.Common.AppDirectory + "zh_cn.lang");
            //实例化配置更新检测器
            //App.GobalSetting.enableApp = true;

        }
    }

    public class MessageEvents
    {
        public static Dictionary<string, IMessageEvent> MessageEventsContainer { get; set; }

        public static void addMessageEvent(IMessageEvent e)
        {
            if (MessageEventsContainer == null)
            {
                MessageEventsContainer = new Dictionary<string, IMessageEvent>();
            }
            MessageEventsContainer.Add(e.Message, e);
        }
    }

    public class AppMain
    {
        public static VIPInfo VipInfo { get; set; }
        public static GobalSetting GobalSetting { get; set; }
        public static List<HeerCards.HCPool> Pools { get; set; }
        public static List<CostUnit> CostUnits { get; internal set; }
        public static I18nHelper i18NHelper { get; set; }
        public static CommandHelper commandHelper { get; set; }

        public static CQApi Common;

        public static string Version = AppMain.Common?.AppInfo.Version.ToString();

        public static ImageDLCManager ImageDLCManager { get; set; }

    }
}